package vip.zhy.faststart.framework.exception;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import vip.zhy.faststart.framework.domain.Response;

import java.io.PrintWriter;
import java.io.StringWriter;

@Slf4j
@Component
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Throwable.class)
    public Response<?> throwable(Throwable e) {
        log.error("server error", e);
        return Response.error(print(e));
    }

    private String print(Throwable e) {
        StringWriter sw = new StringWriter();
        try (PrintWriter pw = new PrintWriter(sw)) {
            e.printStackTrace(pw);
        }
        return sw.toString();
    }

}
